﻿using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Xml.Linq;
using FirstFloor.Documents.Annotations;
using FirstFloor.Documents.Annotations.Storage;

namespace FirstFloor.Documents.Presentation.Annotations
{
    /// <summary>
    /// Represents a text red underline annotation.
    /// </summary>
    public class Underline
        : TextAnnotation
    {
        /// <summary>
        /// Identifies the underline annotation type.
        /// </summary>
        public static readonly XName TypeUnderline = XName.Get("Underline", AnnotationStore.NamespaceAnnotations);

        private Color color = Colors.Red;      // default underline color

        /// <summary>
        /// Initializes a new instance of the <see cref="Underline"/> class.
        /// </summary>
        public Underline()
            : base(TypeUnderline)
        {
        }

        /// <summary>
        /// Gets or sets the underline color.
        /// </summary>
        /// <value>The underline color.</value>
        public Color Color
        {
            get { return this.color; }
            set
            {
                if (this.color != value) {
                    this.color = value;
                    OnPropertyChanged("Color");
                }
            }
        }

        /// <summary>
        /// Renders the annotation for given glyphs element in specified rectangle.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="glyphs">The glyphs.</param>
        /// <param name="rectangle">The rectangle.</param>
        protected override void RenderGlyphsAnnotation(AnnotationRenderContext context, Glyphs glyphs, Rect rectangle)
        {
            var line = new Line() {
                Stroke = new SolidColorBrush(this.Color),
                StrokeThickness = 2,
                X1 = rectangle.Left,
                Y1 = rectangle.Bottom,
                X2 = rectangle.Right,
                Y2 = rectangle.Bottom
            };
            line.StrokeDashArray.Add(2);
            line.StrokeDashArray.Add(1);

            context.AnnotationLayer.Children.Add(line);
        }
    }
}
